//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ksyun.media.shortvideo.kit;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import com.ksyun.media.shortvideo.utils.AuthInfoManager;
import com.ksyun.media.shortvideo.utils.FileUtils;
import com.ksyun.media.shortvideo.utils.ImgBufToTex;
import com.ksyun.media.shortvideo.utils.AuthInfoManager.CheckAuthResultListener;
import com.ksyun.media.streamer.decoder.AVCodecAudioDecoder;
import com.ksyun.media.streamer.decoder.AVCodecVideoDecoder;
import com.ksyun.media.streamer.decoder.Decoder;
import com.ksyun.media.streamer.decoder.MediaCodecVideoDecoder;
import com.ksyun.media.streamer.decoder.Decoder.DecoderErrorListener;
import com.ksyun.media.streamer.decoder.Decoder.DecoderInfoListener;
import com.ksyun.media.streamer.demuxer.AVDemuxerCapture;
import com.ksyun.media.streamer.encoder.AVCodecAudioEncoder;
import com.ksyun.media.streamer.encoder.AVCodecVideoEncoder;
import com.ksyun.media.streamer.encoder.AudioEncodeFormat;
import com.ksyun.media.streamer.encoder.Encoder;
import com.ksyun.media.streamer.encoder.ImgTexToBuf;
import com.ksyun.media.streamer.encoder.MediaCodecSurfaceEncoder;
import com.ksyun.media.streamer.encoder.VideoEncodeFormat;
import com.ksyun.media.streamer.filter.audio.AudioResampleFilter;
import com.ksyun.media.streamer.filter.imgtex.ImgTexMixer;
import com.ksyun.media.streamer.framework.AudioBufFormat;
import com.ksyun.media.streamer.publisher.FilePublisher;
import com.ksyun.media.streamer.publisher.Publisher.PubListener;
import com.ksyun.media.streamer.util.gles.GLRender;

/**
 * 视频转码
 * Rookie
 * 1939428911@qq.com
 * 2019/2/26 15:30
 */
public class MyKSYTranscodeKit {
    private static final String b = "MyKSYTranscodeKit";
    public static final int INFO_DEMUXER_STARTED = 100;
    public static final int INFO_DEMUXER_STOPPED = 101;
    public static final int ERROR_DEMUXER_FAILED = -100;
    public static final int INFO_DECODER_STARTED = 1000;
    public static final int INFO_DECODER_STOPPED = 1001;
    public static final int ERROR_DECODER_FAILED = -1000;
    public static final int INFO_PUBLISHER_STARTED = 2000;
    public static final int INFO_PUBLISHER_COMPLETED = 2001;
    public static final int INFO_PUBLISHER_ABORTED = 2002;
    public static final int ERROR_PUBLISHER_FAILED = -2000;
    public static final float DEFAULT_IFRAME_INTERVAL = 1.0F;
    private float c = 1.0F;
    private int d = 1;
    private int e = 2;
    private int f = 1;
    private int g = 3;
    private int h = 1;
    private float i = 15.0F;
    private int j = 0;
    private int k = 0;
    private int l = 0;
    private int m = 0;
    private int n = 0;
    protected int a = 0;
    private int o = 24;
    private MyKSYTranscodeKit.OnInfoListener p;
    private MyKSYTranscodeKit.OnErrorListener q;
    private AVDemuxerCapture r;
    private AVCodecAudioDecoder s;
    private Decoder t;
    private AVCodecAudioEncoder u;
    private Encoder v;
    private AudioResampleFilter w;
    private FilePublisher x;
    private ImgBufToTex y;
    private ImgTexToBuf z;
    private boolean A;
    private String B;
    private String C;
    private Handler D = new Handler(Looper.getMainLooper());
    private GLRender E = new GLRender();
    private ImgTexMixer F;
    private int G = 0;
    public PubListener mFilePublisherListener = new PubListener() {
        public void onInfo(int var1, long var2) {
            switch(var1) {
            case 1:
                MyKSYTranscodeKit.this.a(2000, (String)null);
                break;
            case 2:
                MyKSYTranscodeKit.this.v.start();
            case 3:
            default:
                break;
            case 4:
                if(!MyKSYTranscodeKit.this.A) {
                    MyKSYTranscodeKit.this.a(2001, MyKSYTranscodeKit.this.C);
                } else {
                    MyKSYTranscodeKit.this.A = false;
                    MyKSYTranscodeKit.this.a(2002, (String)null);
                    FileUtils.deleteFile(MyKSYTranscodeKit.this.C);
                }
            }

        }

        public void onError(int var1, long var2) {
            if(var1 != 0) {
                MyKSYTranscodeKit.this.stop();
            }

            if(MyKSYTranscodeKit.this.q != null) {
                short var4;
                switch(var1) {
                case -4004:
                    var4 = -4004;
                    break;
                case -4003:
                    var4 = -4003;
                    break;
                case -4002:
                    var4 = -4002;
                    break;
                case -4001:
                    var4 = -4001;
                    break;
                default:
                    var4 = -4000;
                }

                MyKSYTranscodeKit.this.a(-2000, (long)var4);
            }

        }
    };
    private com.ksyun.media.streamer.demuxer.AVDemuxerCapture.OnInfoListener H = new com.ksyun.media.streamer.demuxer.AVDemuxerCapture.OnInfoListener() {
        public void onInfo(AVDemuxerCapture var1, int var2, String var3) {
            if(var2 == 0) {
                MyKSYTranscodeKit.this.a(100, (String)null);
            } else if(var2 == 1) {
                MyKSYTranscodeKit.this.a(101, (String)null);
            } else if(var2 == 2) {
                MyKSYTranscodeKit.this.s.setFormatPtr(var1.getAVFormatContextPtr());
                int var4 = var1.getAudioBitrate();
                int var5 = var1.getChannels();
                int var6 = var1.getSampleRate();
                int var7 = var1.getSampleFormat();
                if(MyKSYTranscodeKit.this.k != 0) {
                    var4 = MyKSYTranscodeKit.this.k;
                }

                if(MyKSYTranscodeKit.this.a != 0) {
                    var5 = MyKSYTranscodeKit.this.a;
                } else {
                    MyKSYTranscodeKit.this.a = var5;
                }

                if(MyKSYTranscodeKit.this.n != 0) {
                    var6 = MyKSYTranscodeKit.this.n;
                } else {
                    MyKSYTranscodeKit.this.n = var6;
                }

                MyKSYTranscodeKit.this.a();
                AudioEncodeFormat var8 = new AudioEncodeFormat(256, var7, var6, var5, var4);
                var8.setProfile(MyKSYTranscodeKit.this.f);
                MyKSYTranscodeKit.this.u.configure(var8);
                MyKSYTranscodeKit.this.t.setFormatPtr(var1.getAVFormatContextPtr());
                int var9 = var1.getWidth();
                int var10 = var1.getHeight();
                float var11 = (float)var1.getFrameRate();
                if(MyKSYTranscodeKit.this.l != 0) {
                    var9 = MyKSYTranscodeKit.this.l;
                }

                if(MyKSYTranscodeKit.this.m != 0) {
                    var10 = MyKSYTranscodeKit.this.m;
                }

                if(MyKSYTranscodeKit.this.i > 0.0F) {
                    var11 = (float)((int)MyKSYTranscodeKit.this.i);
                }

                if(var11 <= 0.0F) {
                    var11 = 15.0F;
                }

                if(MyKSYTranscodeKit.this.l == 0 && MyKSYTranscodeKit.this.m == 0) {
                    MyKSYTranscodeKit.this.F.setTargetSize(var9, var10);
                }

                if(MyKSYTranscodeKit.this.j == 0 && MyKSYTranscodeKit.this.g == 2) {
                    MyKSYTranscodeKit.this.setVideoBitrate(600000);
                }

                VideoEncodeFormat var12 = new VideoEncodeFormat(MyKSYTranscodeKit.this.d, var9, var10, MyKSYTranscodeKit.this.j);
                var12.setCrf(MyKSYTranscodeKit.this.o);
                var12.setFramerate(var11);
                var12.setIframeinterval(MyKSYTranscodeKit.this.c);
                var12.setLiveStreaming(false);
                var12.setScene(0);
                var12.setProfile(MyKSYTranscodeKit.this.e);
                MyKSYTranscodeKit.this.v.configure(var12);
                MyKSYTranscodeKit.this.x.setFramerate(MyKSYTranscodeKit.this.i);
                MyKSYTranscodeKit.this.E.init(var9, var10);
            }

        }
    };
    private com.ksyun.media.streamer.demuxer.AVDemuxerCapture.OnErrorListener I = new com.ksyun.media.streamer.demuxer.AVDemuxerCapture.OnErrorListener() {
        public void onError(AVDemuxerCapture var1, int var2, long var3) {
            MyKSYTranscodeKit.this.a(-100, (long)var2);
        }
    };
    private DecoderInfoListener J = new DecoderInfoListener() {
        public void onInfo(Decoder var1, int var2, int var3) {
            if(var2 == 1) {
                MyKSYTranscodeKit.this.a(1000, (String)null);
            } else if(var2 == 2) {
                MyKSYTranscodeKit.this.a(1001, (String)null);
            }

        }
    };
    private DecoderErrorListener K = new DecoderErrorListener() {
        public void onError(Decoder var1, int var2) {
            MyKSYTranscodeKit.this.a(-1000, (long)var2);
        }
    };
    private DecoderInfoListener L = new DecoderInfoListener() {
        public void onInfo(Decoder var1, int var2, int var3) {
            if(var2 == 1) {
                MyKSYTranscodeKit.this.a(1000, (String)null);
            } else if(var2 == 2) {
                MyKSYTranscodeKit.this.a(1001, (String)null);
            }

        }
    };
    private DecoderErrorListener M = new DecoderErrorListener() {
        public void onError(Decoder var1, int var2) {
            MyKSYTranscodeKit.this.a(-1000, (long)var2);
        }
    };
    private CheckAuthResultListener N = new CheckAuthResultListener() {
        public void onAuthResult(int var1) {
            AuthInfoManager.getInstance().removeAuthResultListener(MyKSYTranscodeKit.this.N);
            Log.d("KSYTranscodeKit", "auth success start transcode:" + MyKSYTranscodeKit.this.B);
                MyKSYTranscodeKit.this.x.setUrl(MyKSYTranscodeKit.this.C);
                MyKSYTranscodeKit.this.r.start(MyKSYTranscodeKit.this.B);
        }
    };

    public MyKSYTranscodeKit() {
        this.E.init(1, 1);
        this.y = new ImgBufToTex(this.E);
        this.z = new ImgTexToBuf(this.E);
        this.z.setOutputColorFormat(3);
        this.F = new ImgTexMixer(this.E);
        ImgTexMixer var10002 = this.F;
        this.F.setScalingMode(this.G, 2);
        this.w = new AudioResampleFilter();
        this.r = new AVDemuxerCapture();
        this.r.setOnInfoListener(this.H);
        this.r.setOnErrorListener(this.I);
        this.s = new AVCodecAudioDecoder();
        this.s.setAutoWork(true);
        this.s.setDecoderInfoListener(this.J);
        this.s.setDecoderErrorListener(this.K);
        this.t = new AVCodecVideoDecoder();
        this.t.setAutoWork(true);
        this.t.setDecoderInfoListener(this.L);
        this.t.setDecoderErrorListener(this.M);
        this.u = new AVCodecAudioEncoder();
        this.u.setAutoWork(true);
        this.u.setUseSyncMode(true);
        this.v = new AVCodecVideoEncoder();
        this.v.setAutoWork(true);
        this.v.setUseSyncMode(true);
        this.x = new FilePublisher();
        this.x.setPubListener(this.mFilePublisherListener);
        this.r.getAudioSrcPin().connect(this.s.mSinkPin);
        this.r.getVideoSrcPin().connect(this.d().mSinkPin);
        this.s.mSrcPin.connect(this.w.getSinkPin());
        this.w.getSrcPin().connect(this.u.mSinkPin);
        this.d().mSrcPin.connect(this.y.getSinkPin());
        this.y.mSrcPin.connect(this.F.getSinkPin(this.G));
        this.F.getSrcPin().connect(this.z.getSinkPin());
        this.z.getSrcPin().connect(this.v.mSinkPin);
        this.u.mSrcPin.connect(this.x.getAudioSink());
        this.v.mSrcPin.connect(this.x.getVideoSink());
    }

    private AVCodecVideoEncoder b() {
        return (AVCodecVideoEncoder)this.v;
    }

    private MediaCodecSurfaceEncoder c() {
        return (MediaCodecSurfaceEncoder)this.v;
    }

    private AVCodecVideoDecoder d() {
        return (AVCodecVideoDecoder)this.t;
    }

    private MediaCodecVideoDecoder e() {
        return (MediaCodecVideoDecoder)this.t;
    }

    public void release() {
        if(this.D != null) {
            this.D.removeCallbacksAndMessages((Object)null);
            this.D = null;
        }

        AuthInfoManager.getInstance().removeAuthResultListener(this.N);
        this.r.release();
        this.x.release();
        if(this.E != null) {
            this.E.release();
        }

    }

    public void start(String var1, String var2) {
        if(!TextUtils.isEmpty(var1) && !TextUtils.isEmpty(var2)) {
            if(!FileUtils.isSupportedFile(var1)) {
                throw new IllegalArgumentException("the file must be mp4 or 3gpp or mov");
            } else {
                this.B = var1;
                this.C = var2;
                AuthInfoManager.getInstance().addAuthResultListener(this.N);
                AuthInfoManager.getInstance().checkAuth();
            }
        } else {
            throw new IllegalArgumentException("srcUrl or desUrl can not be null");
        }
    }

    private void a(final int var1, final String var2) {
        if(this.D != null) {
            this.D.post(new Runnable() {
                public void run() {
                    if(MyKSYTranscodeKit.this.p != null) {
                        MyKSYTranscodeKit.this.p.onInfo(MyKSYTranscodeKit.this, var1, var2);
                    }

                }
            });
        }

    }

    private void a(final int var1, final long var2) {
        if(this.D != null) {
            this.D.post(new Runnable() {
                public void run() {
                    if(MyKSYTranscodeKit.this.q != null) {
                        MyKSYTranscodeKit.this.q.onError(MyKSYTranscodeKit.this, var1, var2);
                    }

                }
            });
        }

    }

    public void stop() {
        this.A = true;
        this.r.stop();
    }

    public void setVideoFps(float var1) {
        this.i = var1;
    }

    public float getVideoFps() {
        return this.i;
    }

    public void setVideoBitrate(int var1) {
        this.j = var1;
    }

    public int getVideoBitrate() {
        return this.j;
    }

    public void setVideoKBitrate(int var1) {
        this.setVideoBitrate(var1 * 1000);
    }

    public void setAudioBitrate(int var1) {
        if(var1 < 0) {
            throw new IllegalArgumentException("the AudioBitrate must >=0");
        } else {
            this.k = var1;
        }
    }

    public int getAudioBitrate() {
        return this.k;
    }

    public void setAudioKBitrate(int var1) {
        this.setAudioBitrate(var1 * 1000);
    }

    public void setVideoCodecId(int var1) {
        if(var1 != 1 && var1 != 2 && var1 != 3) {
            throw new IllegalArgumentException("input video codecid error");
        } else {
            this.d = var1;
        }
    }

    public int getVideoCodecId() {
        return this.d;
    }

    public void setOnInfoListener(MyKSYTranscodeKit.OnInfoListener var1) {
        this.p = var1;
    }

    public void setOnErrorListener(MyKSYTranscodeKit.OnErrorListener var1) {
        this.q = var1;
    }

    public void setEncodeMethod(int var1) {
        if(!this.b(var1)) {
            throw new IllegalArgumentException();
        } else {
            this.setVideoEncodeMethod(var1);
            this.a(var1);
        }
    }

    private void a(int var1) {
        if(!this.b(var1)) {
            throw new IllegalArgumentException();
        }
    }

    public void setVideoEncodeMethod(int var1) {
        if(!this.b(var1)) {
            throw new IllegalArgumentException();
        } else if(this.g != var1) {
            if(this.v.isEncoding()) {
                throw new IllegalStateException("Cannot set encode method while composing!");
            } else {
                if(this.g == 3) {
                    this.F.getSrcPin().disconnect(this.z.getSinkPin(), false);
                    this.z.getSrcPin().disconnect(this.b().mSinkPin, false);
                    this.b().mSrcPin.disconnect(this.x.getVideoSink(), false);
                    this.v.release();
                    this.v = new MediaCodecSurfaceEncoder(this.E);
                    this.v.setAutoWork(true);
                    this.v.setUseSyncMode(true);
                    this.v.setEnableTransWorkMode(true);
                    this.F.getSrcPin().connect(this.c().mSinkPin);
                    this.c().mSrcPin.connect(this.x.getVideoSink());
                } else if(this.g == 2) {
                    this.F.getSrcPin().disconnect(this.c().mSinkPin, false);
                    this.c().mSrcPin.disconnect(this.x.getVideoSink(), false);
                    this.v = new AVCodecVideoEncoder();
                    this.v.setAutoWork(true);
                    this.v.setUseSyncMode(true);
                    this.F.getSrcPin().connect(this.z.getSinkPin());
                    this.z.getSrcPin().connect(this.b().mSinkPin);
                    this.b().mSrcPin.connect(this.x.getVideoSink());
                }

                this.g = var1;
            }
        }
    }

    public int getVideoEncodeMethod() {
        return this.g;
    }

    public void setVideoDecodeMethod(int var1) {
        if(var1 != 1 && var1 != 2) {
            throw new IllegalArgumentException("Invalid decode method");
        } else if(this.h != var1) {
            if(this.t.isDecoding()) {
                throw new IllegalStateException("Cannot set decode method while composing!");
            } else {
                if(this.h == 1) {
                    this.r.getVideoSrcPin().disconnect(this.d().mSinkPin, false);
                    this.d().mSrcPin.disconnect(this.y.getSinkPin(), false);
                    this.y.mSrcPin.disconnect(this.F.getSinkPin(this.G), false);
                    this.d().release();
                    this.t = new MediaCodecVideoDecoder(this.E);
                    this.t.setAutoWork(true);
                    this.t.setDecoderInfoListener(this.L);
                    this.t.setDecoderErrorListener(this.M);
                    this.r.getVideoSrcPin().connect(this.e().mSinkPin);
                    this.e().mSrcPin.connect(this.F.getSinkPin(this.G));
                } else if(this.h == 2) {
                    this.r.getVideoSrcPin().disconnect(this.e().mSinkPin, false);
                    this.e().mSrcPin.disconnect(this.F.getSinkPin(this.G), false);
                    this.e().release();
                    this.t = new AVCodecVideoDecoder();
                    this.t.setAutoWork(true);
                    this.t.setDecoderInfoListener(this.L);
                    this.t.setDecoderErrorListener(this.M);
                    this.r.getVideoSrcPin().connect(this.t.mSinkPin);
                    this.t.mSrcPin.connect(this.y.getSinkPin());
                    this.y.mSrcPin.connect(this.F.getSinkPin(this.G));
                }

                this.h = var1;
            }
        }
    }

    public void setVideoEncodeProfile(int var1) {
        this.e = var1;
    }

    public int getVideoEncodeProfile() {
        return this.e;
    }

    public void setAudioSampleRate(int var1) {
        if(var1 < 0) {
            throw new IllegalArgumentException("the AudioSampleRate must > 0");
        } else {
            this.n = var1;
            this.a();
        }
    }

    public void setAudioChannels(int var1) {
        if(var1 != 0 && var1 != 1 && var1 != 2) {
            throw new IllegalArgumentException("the AudioChannels must be mono or stereo");
        } else {
            this.a = var1;
            this.a();
        }
    }

    public void setTargetResolution(int var1, int var2) {
        this.l = var1;
        this.m = var2;
        if(this.l != 0 && this.m != 0) {
            this.F.setTargetSize(this.l, this.m);
        }

    }

    public void setIFrameInterval(float var1) {
        this.c = var1;
    }

    public void setVideoCrf(int var1) {
        this.o = var1;
    }

    public float getProgress() {
        return this.r.getProgress() * 100.0F;
    }

    private int a(int var1, int var2) {
        return (var1 + var2 - 1) / var2 * var2;
    }

    private boolean b(int var1) {
        return var1 == 3 || var1 == 1 || var1 == 2;
    }

    protected void a() {
        if(this.n != 0 && this.a != 0) {
            this.w.setOutFormat(new AudioBufFormat(1, this.n, this.a));
        }

    }

    public interface OnErrorListener {
        void onError(MyKSYTranscodeKit var1, int var2, long var3);
    }

    public interface OnInfoListener {
        void onInfo(MyKSYTranscodeKit var1, int var2, String var3);
    }
}
